1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License.
18   */
19  package groovy.lang;
20  
21  import groovy.util.GroovyTestCase;
22  import org.codehaus.groovy.runtime.InvokerHelper;
23  
24  import java.util.ArrayList;
25  import java.util.Iterator;
26  import java.util.List;
27  
28  /**
29   * @author <a href="mailto:james@coredevelopers.net">James Strachan</a>
30   */
31  public class MetaClassTest extends GroovyTestCase {
32  
33      public void testMetaClass() {
34          Class foo = String[].class;
35          System.out.println(foo + " name: " + foo.getName());
36  
37          MetaClass metaClass = InvokerHelper.getMetaClass(this);
38  
39          assertTrue("got metaclass", metaClass != null);
40  
41          metaClass.invokeMethod(this, "doSomething", new Object[0]);
42      }
43  
44      public void testArray() {
45          String[] value = new String[]{"hello"};
46  
47          MetaClass metaClass = InvokerHelper.getMetaClass(value);
48  
49          assertTrue("got metaclass", metaClass != null);
50  
51          metaClass.invokeMethod(value, "toString", new Object[0]);
52      }
53  
54      public void testString() {
55          String value = "hello";
56  
57          MetaClass metaClass = InvokerHelper.getMetaClass(value);
58  
59          assertTrue("got metaclass", metaClass != null);
60  
61          Object answer = metaClass.invokeMethod(value, "toString", new Object[0]);
62  
63          assertEquals("hello", answer);
64      }
65  
66      public void testObject() {
67          Object value = new Object();
68  
69          MetaClass metaClass = InvokerHelper.getMetaClass(value);
70  
71          assertTrue("got metaclass", metaClass != null);
72  
73          metaClass.invokeMethod(value, "toString", new Object[0]);
74      }
75  
76      public void testPublicField() {
77          DymmyClass dymmyClass = new DymmyClass();
78  
79          MetaClass metaClass = InvokerHelper.getMetaClass(dymmyClass);
80  
81          assertEquals(metaClass.getProperty(dymmyClass, "x"), new Integer(0));
82          assertEquals(metaClass.getProperty(dymmyClass, "y"), "none");
83  
84          metaClass.setProperty(dymmyClass, "x", new Integer(25));
85          assertEquals(dymmyClass.x, 25);
86  
87          metaClass.setProperty(dymmyClass, "y", "newvalue");
88          assertEquals(dymmyClass.y, "newvalue");
89      }
90  
91      public void testSetPropertyWithInt() {
92          DymmyClass dymmyClass = new DymmyClass();
93          MetaClass metaClass = InvokerHelper.getMetaClass(dymmyClass);
94          metaClass.setProperty(dymmyClass, "anInt", new Integer(10));
95      }
96  
97      public void testSetPropertyWithDoubleArray() {
98          DymmyClass dymmyClass = new DymmyClass();
99          MetaClass metaClass = InvokerHelper.getMetaClass(dymmyClass);
100         Double[][] matrix2 =
101                 {
102                         {
103                                 new Double(35), new Double(50), new Double(120)
104                         },
105                         {
106                                 new Double(75), new Double(80), new Double(150)
107                         }
108                 };
109         metaClass.setProperty(dymmyClass, "matrix", matrix2);
110         metaClass.setProperty(dymmyClass, "matrix2", matrix2);
111     }
112 
113     public void testSetPropertyWithArray() {
114         DymmyClass dymmyClass = new DymmyClass();
115         MetaClass metaClass = InvokerHelper.getMetaClass(dymmyClass);
116 
117         // test int[]
118         int[] ints = new int[]{
119                 0, 1, 2, 3
120         };
121         metaClass.setProperty(dymmyClass, "ints", ints);
122         assertEquals(ints, metaClass.getProperty(dymmyClass, "ints"));
123 
124         // test Integer[]
125         Integer[] integers = new Integer[]{
126                 new Integer(0), new Integer(1), new Integer(2), new Integer(3)
127         };
128         metaClass.setProperty(dymmyClass, "integers", integers);
129         assertEquals(integers, metaClass.getProperty(dymmyClass, "integers"));
130     }
131 
132     public void testSetPropertyWithList() {
133         DymmyClass dymmyClass = new DymmyClass();
134         MetaClass metaClass = InvokerHelper.getMetaClass(dymmyClass);
135 
136         // test list
137         ArrayList list = new ArrayList();
138         list.add(new Integer(120));
139         list.add(new Integer(150));
140 
141         // test int[]
142         metaClass.setProperty(dymmyClass, "ints", list);
143 
144         // test Integer[]
145         metaClass.setProperty(dymmyClass, "integers", list);
146     }
147 
148     public void testMetaMethodsOnlyAddedOnce() {
149         MetaClass metaClass = InvokerHelper.getMetaClass("some String");
150 
151         List methods = metaClass.getMetaMethods();
152         for (Iterator iter = methods.iterator(); iter.hasNext();) {
153             MetaMethod method = (MetaMethod) iter.next();
154             int count = 0;
155             for (Iterator inner = methods.iterator(); inner.hasNext();) {
156                 MetaMethod runner = (MetaMethod) inner.next();
157                 if (method.equals(runner)) {
158                     System.out.println("runner = " + runner);
159                     System.out.println("method = " + method);
160                     count++;
161                 }
162             }
163             assertEquals("count of Method " + method.getName(), 1, count);
164         }
165 
166     }
167 
168 
169     public void doSomething() {
170         System.out.println("Called doSomething()");
171     }
172 }
173 
174 
175 class DymmyClass {
176     public int x = 0;
177     public String y = "none";
178 
179     private int anInt;
180     private int[] ints;
181     private Integer[] integers;
182     double[][] matrix2;
183     Double[][] matrix;
184 
185     public Integer[] getIntegers() {
186         return integers;
187     }
188 
189     public void setIntegers(Integer[] integers) {
190         this.integers = integers;
191     }
192 
193     public int[] getInts() {
194         return ints;
195     }
196 
197     public void setInts(int[] ints) {
198         this.ints = ints;
199     }
200 
201     public int getAnInt() {
202         return anInt;
203     }
204 
205     public void setAnInt(int anInt) {
206         this.anInt = anInt;
207     }
208 
209     public void setMatrix(Double[][] matrix) {
210         this.matrix = matrix;
211     }
212 
213     public void setMatrix2(double[][] matrixReloaded) {
214         this.matrix2 = matrixReloaded;
215     }
216 
217 }
218